//
//  DYPageContentView.swift
//  Douyu_Swift
//
//  Created by Albert Zhu on 16/11/1.
//  Copyright © 2016年 Albert Zhu. All rights reserved.
//

import UIKit

private let CellID = "cell"

protocol DYPageContentViewDelegate: class {
    func pageContentView(_ pageContentView: DYPageContentView, scrollProgress: CGFloat)
}

class DYPageContentView: UIView {
    
    weak var delegate: DYPageContentViewDelegate?
    var childVCs: [UIViewController]
    weak var parentVC: UIViewController?
    
    lazy var collectionView: UICollectionView = {[weak self] in
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = self!.bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        
        let clv = UICollectionView(frame: self!.bounds, collectionViewLayout: layout)
        clv.showsHorizontalScrollIndicator = false
        clv.isPagingEnabled = true
        clv.bounces = false
        
        clv.dataSource = self
        clv.delegate = self
        clv.register(UICollectionViewCell.self, forCellWithReuseIdentifier: CellID)
        
        return clv
        }()
    
    init(frame: CGRect, childVCs: [UIViewController], parentController: UIViewController?) {
        self.childVCs = childVCs
        parentVC = parentController
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension DYPageContentView {
    func setupUI() {
        for childVC in childVCs {
            parentVC?.addChildViewController(childVC)
        }
        
        addSubview(collectionView)
        collectionView.frame = bounds
    }
}

extension DYPageContentView: UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return childVCs.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellID, for: indexPath)
        
        for view in cell.contentView.subviews {
            view.removeFromSuperview()
        }
        
        let childVC = childVCs[indexPath.item]
        childVC.view.frame = cell.contentView.bounds
        cell.contentView.addSubview(childVC.view)
        
        return cell
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let progress = scrollView.contentOffset.x / scrollView.bounds.width
                
        delegate?.pageContentView(self, scrollProgress: progress)
    }
}

extension DYPageContentView {
    func setCurrentIndex(_ currentIndex: Int) {
        let indexPath = IndexPath(item: currentIndex, section: 0)
        
        collectionView.scrollToItem(at: indexPath, at: .left, animated: false)
    }
}
